package com.liukai.todo_android.presenter;

import com.liukai.todo_android.common.utils.RetrofitManager;
import com.liukai.todo_android.common.api.UserApi;
import com.liukai.todo_android.common.base.BasePresenter;
import com.liukai.todo_android.call_back.IRegisterCallBack;
import com.liukai.todo_android.entity.vo.LoginVO;
import com.liukai.todo_android.entity.Resp;
import com.liukai.todo_android.entity.vo.RegisterVO;

import java.net.HttpURLConnection;

import javax.net.ssl.HttpsURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * @author ：liukai
 * @date ：Created in 2021/8/9 11:06 上午
 * @description：注册相关的请求
 * @modified By：
 * @version: $
 */
public class RegisterPresenter extends BasePresenter<IRegisterCallBack> {

    public void getCode(String email) {

        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        UserApi userApi = retrofit.create(UserApi.class);
        Call<Resp> getCodeTask = userApi.getCode(email);
        getCodeTask.enqueue(new Callback<Resp>() {
            @Override
            public void onResponse(Call<Resp> call, Response<Resp> response) {
                int code = response.code();
                if (code == HttpURLConnection.HTTP_OK) {
                    Resp body = response.body();
                    if (body.getCode() == 200) {
                        mCallBack.getCodeSuccess(body);
                    } else {
                        mCallBack.getCodeError(body);
                    }
                } else {
                    mCallBack.getCodeError(Resp.err("系统问题，请联系管理员"));
                }
            }

            @Override
            public void onFailure(Call<Resp> call, Throwable t) {
                mCallBack.getCodeError(Resp.err("获取验证码失败，请检查网络是否连接"));
            }
        });

    }

    public void register(RegisterVO registerVO) {

        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        UserApi userApi = retrofit.create(UserApi.class);
        Call<Resp> registerTask = userApi.register(registerVO);
        registerTask.enqueue(new Callback<Resp>() {
            @Override
            public void onResponse(Call<Resp> call, Response<Resp> response) {
                int code = response.code();
                if (code == HttpsURLConnection.HTTP_OK) {
                    Resp body = response.body();
                    if (body.getCode() == 200) {
                        mCallBack.registerSuccess(body);
                    } else {
                        mCallBack.registerError(body);
                    }
                } else {
                    mCallBack.registerError(Resp.err("系统问题，请联系管理员"));
                }
            }

            @Override
            public void onFailure(Call<Resp> call, Throwable t) {
                mCallBack.getCodeError(Resp.err("注册失败，请检查网络是否连接"));
            }
        });

    }

}
